﻿Imports System.IO
Imports System.Data


Partial Class incidencias_ucExportar
    Inherits System.Web.UI.UserControl

    'Public Sub loadGridView(ByVal pobjDt As DataTable, ByVal lx_archivo As String)

    '    Exportar_reg_Grilla_FOR(pobjDt, lx_archivo)

    'End Sub

    Public Sub Exportar_reg_CSS_Grilla(ByVal objData As DataTable, ByVal lx_archivo As String)

        If objData IsNot Nothing Then
            If objData.Rows.Count() > 0 Then
                Dim dt As DataTable = objData
                Dim GridView1 As New GridView()
                GridView1.AllowPaging = False
                GridView1.DataSource = dt
                GridView1.DataBind()
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = "application/vnd.ms-excel"
                Response.AddHeader("Content-Disposition", "attachment;filename=" + lx_archivo + ".xls")
                Response.Charset = ""
                Dim sw As New StringWriter()
                Dim hw As New HtmlTextWriter(sw)
                GridView1.RenderControl(hw)
                Response.Output.Write(sw.ToString())
                Response.Flush()
                Response.End()
            Else
                objData = Nothing
            End If
        Else
            objData = Nothing
        End If

    End Sub

    Public Sub Exportar_reg_Grilla_FOR(ByVal objData As DataTable, ByVal lx_archivo As String)

        If objData IsNot Nothing Then
            If objData.Rows.Count() > 0 Then
                Dim attachment As String = "attachment; filename=" + lx_archivo + ".xls"
                Response.ClearContent()
                Response.AddHeader("content-disposition", attachment)
                Response.ContentType = "application/vnd.ms-excel"
                Dim tab As String = ""
                For Each dc As DataColumn In objData.Columns
                    Response.Write(tab + dc.ColumnName)
                    tab = vbTab
                Next
                Response.Write(vbLf)

                For Each dr As DataRow In objData.Rows
                    tab = ""
                    For i = 0 To objData.Columns.Count - 1
                        Response.Write(tab + Trim(dr(i).ToString().Replace(Chr(13), "").Replace(Chr(10), "").Replace(Chr(9), "")))
                        tab = vbTab
                    Next
                    Response.Write(vbLf)
                Next
                Response.End()
            Else
                objData = Nothing
            End If
        Else
            objData = Nothing
        End If

    End Sub

End Class
